﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using V5_DBUtility;
using System.Data;
using V5_Model;
using System.Data.SqlClient;

namespace V5_DAL.SqlServer
{
    public class DALGZProductOrderItem
    {
        #region  Method


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelGZProductOrderItem model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Bill_StockOutOrder_Item(");
            strSql.Append("ID,SeqNo,ParentID,ProductID,PackageSpec,OrderNumber,OrderPrice,OrderMoney,PiecePrice,PieceNumber,BulkNumber,Taxrate,Taxamt,Goodsamt,OrderBatchCode,Discount,Produceddate,Validdate,DiscountMoney,DeliverNumber,Remark,Remark1,Remark2,Remark3,Remark4)");
            strSql.Append(" values (");
            strSql.Append("@ID,@SeqNo,@ParentID,@ProductID,@PackageSpec,@OrderNumber,@OrderPrice,@OrderMoney,@PiecePrice,@PieceNumber,@BulkNumber,@Taxrate,@Taxamt,@Goodsamt,@OrderBatchCode,@Discount,@Produceddate,@Validdate,@DiscountMoney,@DeliverNumber,@Remark,@Remark1,@Remark2,@Remark3,@Remark4)");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@SeqNo", SqlDbType.Int,4),
					new SqlParameter("@ParentID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@ProductID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@PackageSpec", SqlDbType.Decimal,13),
					new SqlParameter("@OrderNumber", SqlDbType.Decimal,9),
					new SqlParameter("@OrderPrice", SqlDbType.Decimal,9),
					new SqlParameter("@OrderMoney", SqlDbType.Decimal,13),
					new SqlParameter("@PiecePrice", SqlDbType.Decimal,9),
					new SqlParameter("@PieceNumber", SqlDbType.Decimal,9),
					new SqlParameter("@BulkNumber", SqlDbType.Decimal,9),
					new SqlParameter("@Taxrate", SqlDbType.Decimal,13),
					new SqlParameter("@Taxamt", SqlDbType.Decimal,13),
					new SqlParameter("@Goodsamt", SqlDbType.Decimal,13),
					new SqlParameter("@OrderBatchCode", SqlDbType.VarChar,50),
					new SqlParameter("@Discount", SqlDbType.Decimal,13),
					new SqlParameter("@Produceddate", SqlDbType.DateTime),
					new SqlParameter("@Validdate", SqlDbType.DateTime),
					new SqlParameter("@DiscountMoney", SqlDbType.Decimal,9),
					new SqlParameter("@DeliverNumber", SqlDbType.Decimal,13),
					new SqlParameter("@Remark", SqlDbType.VarChar,4000),
					new SqlParameter("@Remark1", SqlDbType.VarChar,4000),
					new SqlParameter("@Remark2", SqlDbType.VarChar,4000),
					new SqlParameter("@Remark3", SqlDbType.VarChar,4000),
					new SqlParameter("@Remark4", SqlDbType.VarChar,4000) };
            parameters[0].Value = Guid.NewGuid();
            parameters[1].Value = model.SeqNo;
            parameters[2].Value = model.ParentID;
            parameters[3].Value = model.ProductID;
            parameters[4].Value = model.PackageSpec;
            parameters[5].Value = model.OrderNumber;
            parameters[6].Value = model.OrderPrice;
            parameters[7].Value = model.OrderMoney;
            parameters[8].Value = model.PiecePrice;
            parameters[9].Value = model.PieceNumber;
            parameters[10].Value = model.BulkNumber;
            parameters[11].Value = model.Taxrate;
            parameters[12].Value = model.Taxamt;
            parameters[13].Value = model.Goodsamt;
            parameters[14].Value = model.OrderBatchCode;
            parameters[15].Value = model.Discount;
            parameters[16].Value = null;
            parameters[17].Value = null;
            parameters[18].Value = model.DiscountMoney;
            parameters[19].Value = model.DeliverNumber;
            parameters[20].Value = model.Remark;
            parameters[21].Value = model.Remark1;
            parameters[22].Value = model.Remark2;
            parameters[23].Value = model.Remark3;
            parameters[24].Value = model.Remark4; 

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ModelGZProductOrderItem model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Bill_StockOutOrder_Item set ");
            strSql.Append("SeqNo=@SeqNo,");
            strSql.Append("ParentID=@ParentID,");
            strSql.Append("ProductID=@ProductID,");
            strSql.Append("PackageSpec=@PackageSpec,");
            strSql.Append("OrderNumber=@OrderNumber,");
            strSql.Append("OrderPrice=@OrderPrice,");
            strSql.Append("OrderMoney=@OrderMoney,");
            strSql.Append("PiecePrice=@PiecePrice,");
            strSql.Append("PieceNumber=@PieceNumber,");
            strSql.Append("BulkNumber=@BulkNumber,");
            strSql.Append("Taxrate=@Taxrate,");
            strSql.Append("Taxamt=@Taxamt,");
            strSql.Append("Goodsamt=@Goodsamt,");
            strSql.Append("OrderBatchCode=@OrderBatchCode,");
            strSql.Append("Discount=@Discount,");
            strSql.Append("Produceddate=@Produceddate,");
            strSql.Append("Validdate=@Validdate,");
            strSql.Append("DiscountMoney=@DiscountMoney,");
            strSql.Append("DeliverNumber=@DeliverNumber,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("Remark1=@Remark1,");
            strSql.Append("Remark2=@Remark2,");
            strSql.Append("Remark3=@Remark3,");
            strSql.Append("Remark4=@Remark4");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@SeqNo", SqlDbType.Int,4),
					new SqlParameter("@ParentID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@ProductID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@PackageSpec", SqlDbType.Decimal,13),
					new SqlParameter("@OrderNumber", SqlDbType.Decimal,9),
					new SqlParameter("@OrderPrice", SqlDbType.Decimal,9),
					new SqlParameter("@OrderMoney", SqlDbType.Decimal,13),
					new SqlParameter("@PiecePrice", SqlDbType.Decimal,9),
					new SqlParameter("@PieceNumber", SqlDbType.Decimal,9),
					new SqlParameter("@BulkNumber", SqlDbType.Decimal,9),
					new SqlParameter("@Taxrate", SqlDbType.Decimal,13),
					new SqlParameter("@Taxamt", SqlDbType.Decimal,13),
					new SqlParameter("@Goodsamt", SqlDbType.Decimal,13),
					new SqlParameter("@OrderBatchCode", SqlDbType.VarChar,50),
					new SqlParameter("@Discount", SqlDbType.Decimal,13),
					new SqlParameter("@Produceddate", SqlDbType.DateTime),
					new SqlParameter("@Validdate", SqlDbType.DateTime),
					new SqlParameter("@DiscountMoney", SqlDbType.Decimal,9),
					new SqlParameter("@DeliverNumber", SqlDbType.Decimal,13),
					new SqlParameter("@Remark", SqlDbType.VarChar,4000),
					new SqlParameter("@Remark1", SqlDbType.VarChar,4000),
					new SqlParameter("@Remark2", SqlDbType.VarChar,4000),
					new SqlParameter("@Remark3", SqlDbType.VarChar,4000),
					new SqlParameter("@Remark4", SqlDbType.VarChar,4000),
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier,16)};
            parameters[0].Value = model.SeqNo;
            parameters[1].Value = model.ParentID;
            parameters[2].Value = model.ProductID;
            parameters[3].Value = model.PackageSpec;
            parameters[4].Value = model.OrderNumber;
            parameters[5].Value = model.OrderPrice;
            parameters[6].Value = model.OrderMoney;
            parameters[7].Value = model.PiecePrice;
            parameters[8].Value = model.PieceNumber;
            parameters[9].Value = model.BulkNumber;
            parameters[10].Value = model.Taxrate;
            parameters[11].Value = model.Taxamt;
            parameters[12].Value = model.Goodsamt;
            parameters[13].Value = model.OrderBatchCode;
            parameters[14].Value = model.Discount;
            parameters[15].Value = model.Produceddate;
            parameters[16].Value = model.Validdate;
            parameters[17].Value = model.DiscountMoney;
            parameters[18].Value = model.DeliverNumber;
            parameters[19].Value = model.Remark;
            parameters[20].Value = model.Remark1;
            parameters[21].Value = model.Remark2;
            parameters[22].Value = model.Remark3;
            parameters[23].Value = model.Remark4;
            parameters[24].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(Guid ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Bill_StockOutOrder_Item ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier,16)};
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Bill_StockOutOrder_Item ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelGZProductOrderItem GetModel(Guid ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,SeqNo,ParentID,ProductID,PackageSpec,OrderNumber,OrderPrice,OrderMoney,PiecePrice,PieceNumber,BulkNumber,Taxrate,Taxamt,Goodsamt,OrderBatchCode,Discount,Produceddate,Validdate,DiscountMoney,DeliverNumber,Remark,Remark1,Remark2,Remark3,Remark4,TimeStamp from Bill_StockOutOrder_Item ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier,16)};
            parameters[0].Value = ID;

            ModelGZProductOrderItem model = new ModelGZProductOrderItem();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = new Guid(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SeqNo"] != null && ds.Tables[0].Rows[0]["SeqNo"].ToString() != "")
                {
                    model.SeqNo = int.Parse(ds.Tables[0].Rows[0]["SeqNo"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ParentID"] != null && ds.Tables[0].Rows[0]["ParentID"].ToString() != "")
                {
                    model.ParentID = new Guid(ds.Tables[0].Rows[0]["ParentID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProductID"] != null && ds.Tables[0].Rows[0]["ProductID"].ToString() != "")
                {
                    model.ProductID = new Guid(ds.Tables[0].Rows[0]["ProductID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PackageSpec"] != null && ds.Tables[0].Rows[0]["PackageSpec"].ToString() != "")
                {
                    model.PackageSpec = decimal.Parse(ds.Tables[0].Rows[0]["PackageSpec"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OrderNumber"] != null && ds.Tables[0].Rows[0]["OrderNumber"].ToString() != "")
                {
                    model.OrderNumber = decimal.Parse(ds.Tables[0].Rows[0]["OrderNumber"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OrderPrice"] != null && ds.Tables[0].Rows[0]["OrderPrice"].ToString() != "")
                {
                    model.OrderPrice = decimal.Parse(ds.Tables[0].Rows[0]["OrderPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OrderMoney"] != null && ds.Tables[0].Rows[0]["OrderMoney"].ToString() != "")
                {
                    model.OrderMoney = decimal.Parse(ds.Tables[0].Rows[0]["OrderMoney"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PiecePrice"] != null && ds.Tables[0].Rows[0]["PiecePrice"].ToString() != "")
                {
                    model.PiecePrice = decimal.Parse(ds.Tables[0].Rows[0]["PiecePrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PieceNumber"] != null && ds.Tables[0].Rows[0]["PieceNumber"].ToString() != "")
                {
                    model.PieceNumber = decimal.Parse(ds.Tables[0].Rows[0]["PieceNumber"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BulkNumber"] != null && ds.Tables[0].Rows[0]["BulkNumber"].ToString() != "")
                {
                    model.BulkNumber = decimal.Parse(ds.Tables[0].Rows[0]["BulkNumber"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Taxrate"] != null && ds.Tables[0].Rows[0]["Taxrate"].ToString() != "")
                {
                    model.Taxrate = decimal.Parse(ds.Tables[0].Rows[0]["Taxrate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Taxamt"] != null && ds.Tables[0].Rows[0]["Taxamt"].ToString() != "")
                {
                    model.Taxamt = decimal.Parse(ds.Tables[0].Rows[0]["Taxamt"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Goodsamt"] != null && ds.Tables[0].Rows[0]["Goodsamt"].ToString() != "")
                {
                    model.Goodsamt = decimal.Parse(ds.Tables[0].Rows[0]["Goodsamt"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OrderBatchCode"] != null && ds.Tables[0].Rows[0]["OrderBatchCode"].ToString() != "")
                {
                    model.OrderBatchCode = ds.Tables[0].Rows[0]["OrderBatchCode"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Discount"] != null && ds.Tables[0].Rows[0]["Discount"].ToString() != "")
                {
                    model.Discount = decimal.Parse(ds.Tables[0].Rows[0]["Discount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Produceddate"] != null && ds.Tables[0].Rows[0]["Produceddate"].ToString() != "")
                {
                    model.Produceddate = DateTime.Parse(ds.Tables[0].Rows[0]["Produceddate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Validdate"] != null && ds.Tables[0].Rows[0]["Validdate"].ToString() != "")
                {
                    model.Validdate = DateTime.Parse(ds.Tables[0].Rows[0]["Validdate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DiscountMoney"] != null && ds.Tables[0].Rows[0]["DiscountMoney"].ToString() != "")
                {
                    model.DiscountMoney = decimal.Parse(ds.Tables[0].Rows[0]["DiscountMoney"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DeliverNumber"] != null && ds.Tables[0].Rows[0]["DeliverNumber"].ToString() != "")
                {
                    model.DeliverNumber = decimal.Parse(ds.Tables[0].Rows[0]["DeliverNumber"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Remark"] != null && ds.Tables[0].Rows[0]["Remark"].ToString() != "")
                {
                    model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Remark1"] != null && ds.Tables[0].Rows[0]["Remark1"].ToString() != "")
                {
                    model.Remark1 = ds.Tables[0].Rows[0]["Remark1"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Remark2"] != null && ds.Tables[0].Rows[0]["Remark2"].ToString() != "")
                {
                    model.Remark2 = ds.Tables[0].Rows[0]["Remark2"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Remark3"] != null && ds.Tables[0].Rows[0]["Remark3"].ToString() != "")
                {
                    model.Remark3 = ds.Tables[0].Rows[0]["Remark3"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Remark4"] != null && ds.Tables[0].Rows[0]["Remark4"].ToString() != "")
                {
                    model.Remark4 = ds.Tables[0].Rows[0]["Remark4"].ToString();
                }
                //if (ds.Tables[0].Rows[0]["TimeStamp"] != null && ds.Tables[0].Rows[0]["TimeStamp"].ToString() != "")
                //{
                //    model.TimeStamp = DateTime.Parse(ds.Tables[0].Rows[0]["TimeStamp"].ToString());
                //}
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,SeqNo,ParentID,ProductID,PackageSpec,OrderNumber,OrderPrice,OrderMoney,PiecePrice,PieceNumber,BulkNumber,Taxrate,Taxamt,Goodsamt,OrderBatchCode,Discount,Produceddate,Validdate,DiscountMoney,DeliverNumber,Remark,Remark1,Remark2,Remark3,Remark4,TimeStamp ");
            strSql.Append(" FROM Bill_StockOutOrder_Item ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,SeqNo,ParentID,ProductID,PackageSpec,OrderNumber,OrderPrice,OrderMoney,PiecePrice,PieceNumber,BulkNumber,Taxrate,Taxamt,Goodsamt,OrderBatchCode,Discount,Produceddate,Validdate,DiscountMoney,DeliverNumber,Remark,Remark1,Remark2,Remark3,Remark4,TimeStamp ");
            strSql.Append(" FROM Bill_StockOutOrder_Item ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        public string getOrderPrice(string id)
        {
            string sql = @"SELECT  sum(OrderMoney)
                          FROM Bill_StockOutOrder_Item
                          WHERE ParentID = '"+id+"'";

            object t = DbHelperSQL.GetSingle(sql);


            if (t == null)
                return "";
            else
                return t.ToString();


        }

       
         //<summary>
         //分页获取数据列表
         //</summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Bill_StockOutOrder_Item";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }

        #endregion  Method
    }
}